<?php

namespace common\jobs\handle;

use Yii;
use yii\base\Exception;
use common\models\shop\ShopOrderUnified;
use common\logics\order\handle\OrderCancelHandle;

/**
 * 商城订单队列操作
 *
 */
class OrderHandle extends \common\base\Model {

    /**
     * 取消过期订单
     * @param type $id
     * @param type $msg
     * @return boolean
     */
    public static function cancelExpired($id, &$msg) {
        try {
            $unifiedOrder = ShopOrderUnified::findOrFail($id);
        } catch (Exception $e) {
            $msg = $e->getMessage();
            return false;
        }
        if (!$unifiedOrder->canCancel || !$unifiedOrder->isExpired) {
            $msg = '订单状态不符合取消标准';
            return false;
        }
        try {
            $model = new OrderCancelHandle();
            $model->user_id = $unifiedOrder->user_id;
            $model->order_id = $unifiedOrder->id;
            $model->operator = 0;
            $order = $model->save();
        } catch (Exception $e) {
            $msg = $e->getMessage();
            return false;
        }
        Yii::error($order);
        return true;
    }

}
